<template>
	<f7-page>
		<f7-card>
			<f7-card-header>
				<span>{{member.name + '(' + member.xgh + ')'}}</span>
				<span style="text-align:right;">{{member.type}}</span>
			</f7-card-header>
			<f7-card-content>
				<p>单位：{{member.department}}</p>
				<p>楼栋：{{member.domitories.map(domitory => domitory.schoolDistrict + '-' + domitory.name).join('、')}}</p>
				<f7-button style="font-size:20px;" @click="scanQRCode" raised>
					<f7-icon f7="qrcode_viewfinder"></f7-icon>&nbsp;&nbsp;&nbsp;扫一扫
				</f7-button>
			</f7-card-content>
		</f7-card>
		<f7-card>
			<f7-card-header style="font-size:20px; font-weight:bold;">
				<span>{{resultTitle}}</span>
			</f7-card-header>
			<f7-card-content v-if="domitory.id != null" :class="valid ? 'pass' : 'forbid'" 
				style="padding-top:50px; padding-bottom:50px; text-align:center; color:white; font-weight:bold; border-radius:10px;">
				<p style="font-size:24px;" 
					v-text="valid ? '正常通行' : '禁止通行'">
				</p>
				<div style="font-size:18px;">
					<span style="background-color:#D1D1D1;">{{dateToStr('MM', new Date())}}</span>月
					<span style="background-color:#D1D1D1;">{{dateToStr('DD', new Date())}}</span>日
					<span style="background-color:#D1D1D1;">{{dateToStr('HH', new Date())}}</span>时
					<span style="background-color:#D1D1D1;">{{dateToStr('mm', new Date())}}</span>分
				</div>
			</f7-card-content>
		</f7-card>
	</f7-page>
</template>

<script>
import Common from '@/utils/Common.js';
import DateUtil from '@/utils/DateUtil.js';
import request from '@/utils/request.js';
import { getUser } from '@/utils/auth.js'; 

export default {
	data: function() {
		return {
			member: {
				xgh: null,
				name: null,
				department: null,
				type: null,
				domitories: []
			},
			domitory: {
				id: null,
				schoolDistrict: null,
				name: null
			},
			valid: false
		}
	},
	methods: {
		scanQRCode() {
			let self = this;
			window.wx.scanQRCode({
				desc: '扫描门禁二维码',
				needResult: 1, 
				scanType: ["qrCode"],
				success: function(res) {
					let result = res.resultStr;
					let reg = new RegExp('redirect_uri=http%3a%2f%2fyqfkmj.gznu.edu.cn[-%.a-zA-Z0-9]+&');
					let match = result.match(reg);
					if(match == null) {
						self.$f7.dialog.alert('二维码格式错误', '');
						return;
					}
					result = match[0].split('=')[1];
					result = decodeURIComponent(result);
					match = result.split('?');
					if(match.length == 1) {
						self.$f7.dialog.alert('二维码格式错误', '');
						return;
					}
					result = match[1];
					self.domitory.id = Common.getQueryVariable(result, 'domitoryId');
				},
				error: function(res) {
					if(res.errMsg.indexOf('function_not_exist') > 0) {
						alert('版本过低请升级')
					} else {
						alert('发生错误！');
					}
				}
			});
		}, 
		dateToStr(formatStr, date) {
			return DateUtil.dateToStr(formatStr, date);
		}
	},
	computed: {
		resultTitle() {
			if(this.domitory.id == null)
				return '请扫描门禁二维码'
			let title = '';
			if(this.domitory.schoolDistrict != null) {
				title += this.domitory.schoolDistrict + '-';
			}
			title += this.domitory.name;
			return title;
		}
	},
	watch: {
		'member.xgh': function() {
			let self = this;
			request({
				url: 'api/wisedu/ldap/member/findByXgh',
				params: {xgh: self.member.xgh}
			}).then(data => {
				let member = data.element;
				self.member.name = member.xm;
				self.member.type = member.type;
				return request({
					url: 'api/wisedu/jpa/zxbz/zxbzDw/search',
					params: {dm: data.element.dwdm}
				})
			}).then(data => {
				self.member.department = data.list[0].mc;
			}).catch(error => {
				console.error(error);
			});
			
			request({
				url: 'api/userDomitory/search',
				params: {xgh: self.member.xgh}
			}).then(data => {
				let ids = data.list.map(userDomitory => userDomitory.domitoryId);
				if(ids.length == 0) {
					ids.push('tttttttt');
				}
				return request({
					url: 'api/domitory/search',
					params: {ids}
				});
			}).then(data => {
				self.member.domitories = data.list;
			}).catch(error => {
				console.error(error);
			});
		},
		'domitory.id': function() {
			let self = this;
			request({
				url: 'api/domitory/search',
				params: {id: self.domitory.id}
			}).then(data => {
				self.domitory.name = data.list[0].name;
				self.domitory.schoolDistrict = data.list[0].schoolDistrict;
			}).catch(error => {
				console.error(error);
			});
			
			let dialog = self.$f7.dialog.preloader('验证权限中...');
			request({
				url: 'api/userDomitory/check',
				method: 'post',
				data: {xgh: self.member.xgh, domitoryId: self.domitory.id},
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				}
			}).then(data => {
				self.valid = true;
				dialog.close();
			}).catch(error => {
				self.valid = false;
				self.$f7.dialog.alert(error.detail.msg, '');
				dialog.close();
				console.error(error);
			});
		}
	},
	created() {
		this.member.xgh = getUser().xgh;
		this.domitory.id = Common.getQueryVariable(window.location.search, 'domitoryId');
	}
}
</script>

<style scoped>
.card .forbid {
	background-color: #DD2222;
	background-image: linear-gradient(135deg, #eb9c1c 10%,#da0641 100%);
}

.card .pass {
	background-color: #09F709;
	background-image: linear-gradient(135deg, #81FBB8 10%, #28C76F 100%);
}
</style>